<template>
  <div class="card">
    <h2>用户信息</h2>
    
    <!-- 用户表单 -->
    <form @submit.prevent="saveUser" class="user-form">
      <div class="form-group">
        <label for="name">姓名:</label>
        <input 
          id="name"
          v-model="user.name" 
          type="text" 
          class="form-input"
          placeholder="请输入姓名"
          required
        />
      </div>
      
      <div class="form-group">
        <label for="email">邮箱:</label>
        <input 
          id="email"
          v-model="user.email" 
          type="email" 
          class="form-input"
          placeholder="请输入邮箱"
          required
        />
      </div>
      
      <div class="form-group">
        <label for="age">年龄:</label>
        <input 
          id="age"
          v-model.number="user.age" 
          type="number" 
          class="form-input"
          placeholder="请输入年龄"
          min="1"
          max="120"
        />
      </div>
      
      <div class="form-group">
        <label>性别:</label>
        <div class="radio-group">
          <label class="radio-label">
            <input 
              type="radio" 
              v-model="user.gender" 
              value="male"
            />
            男
          </label>
          <label class="radio-label">
            <input 
              type="radio" 
              v-model="user.gender" 
              value="female"
            />
            女
          </label>
        </div>
      </div>
      
      <div class="form-group">
        <label for="bio">个人简介:</label>
        <textarea 
          id="bio"
          v-model="user.bio" 
          class="form-textarea"
          placeholder="请输入个人简介"
          rows="3"
        ></textarea>
      </div>
      
      <button type="submit" class="btn btn-primary">保存信息</button>
    </form>
    
    <!-- 用户信息展示 -->
    <div v-if="isSaved" class="user-display">
      <h3>当前用户信息:</h3>
      <div class="user-info">
        <p><strong>姓名:</strong> {{ user.name }}</p>
        <p><strong>邮箱:</strong> {{ user.email }}</p>
        <p><strong>年龄:</strong> {{ user.age }} 岁</p>
        <p><strong>性别:</strong> {{ genderText }}</p>
        <p><strong>个人简介:</strong> {{ user.bio || '暂无' }}</p>
      </div>
      <p class="save-time">保存时间: {{ saveTime }}</p>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'

// 响应式数据
const user = ref({
  name: '',
  email: '',
  age: null,
  gender: '',
  bio: ''
})

const isSaved = ref(false)
const saveTime = ref('')

// 计算属性
const genderText = computed(() => {
  return user.value.gender === 'male' ? '男' : 
         user.value.gender === 'female' ? '女' : '未选择'
})

// 生命周期钩子
onMounted(() => {
  console.log('UserInfo 组件已挂载')
  
  // 从 localStorage 恢复数据
  const savedUser = localStorage.getItem('vue3-user')
  if (savedUser) {
    user.value = { ...user.value, ...JSON.parse(savedUser) }
    isSaved.value = true
    saveTime.value = localStorage.getItem('vue3-save-time') || ''
  }
})

// 方法
const saveUser = () => {
  // 保存到 localStorage
  localStorage.setItem('vue3-user', JSON.stringify(user.value))
  localStorage.setItem('vue3-save-time', new Date().toLocaleString())
  
  isSaved.value = true
  saveTime.value = new Date().toLocaleString()
  
  console.log('用户信息已保存:', user.value)
}
</script>

<style scoped>
.card {
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.1);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255,255,255,0.2);
}

.card h2 {
  text-align: center;
  margin-bottom: 20px;
  color: #333;
  font-size: 1.5rem;
}

.user-form {
  margin-bottom: 20px;
}

.form-group {
  margin-bottom: 16px;
}

.form-group label {
  display: block;
  margin-bottom: 6px;
  font-weight: bold;
  color: #333;
}

.form-input,
.form-textarea {
  width: 100%;
  padding: 12px;
  border: 2px solid #e9ecef;
  border-radius: 8px;
  font-size: 1rem;
  transition: border-color 0.3s ease;
}

.form-input:focus,
.form-textarea:focus {
  outline: none;
  border-color: #667eea;
}

.form-textarea {
  resize: vertical;
  min-height: 80px;
}

.radio-group {
  display: flex;
  gap: 20px;
}

.radio-label {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  font-weight: normal;
}

.radio-label input[type="radio"] {
  width: 16px;
  height: 16px;
}

.btn {
  width: 100%;
  padding: 12px 20px;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-top: 10px;
}

.btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}

.btn-primary {
  background: #007bff;
  color: white;
}

.user-display {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid #e9ecef;
}

.user-display h3 {
  color: #333;
  margin-bottom: 15px;
  font-size: 1.2rem;
}

.user-info p {
  margin: 8px 0;
  color: #666;
}

.save-time {
  margin-top: 15px;
  font-size: 0.9rem;
  color: #999;
  font-style: italic;
}
</style> 